#include "common.h"

#include "ui.h"

LPD3DXSPRITE UI::_pSprite	= NULL;
DWORD UI::_refCount			= 0;

UI::UI()
{
	++_refCount;
}

UI::~UI()
{
	if( --_refCount == 0 )
	{
		if( _pSprite )
		{
			_pSprite->Release();
			_pSprite = NULL;
		}
	}
}

void UI::Draw()
{
	const LPD3DXSPRITE pSprite = UI::GetSprite();
	if( SUCCEEDED( pSprite->Begin( D3DXSPRITE_ALPHABLEND ) ) )
	{	
		ActualDraw();
		pSprite->End();
	}
}

D3DXVECTOR2 UI::GetSize()
{
	return D3DXVECTOR2(
		static_cast< float >( _rect.right - _rect.left ),
		static_cast< float >( _rect.bottom - _rect.top ) );
}

LPD3DXSPRITE UI::GetSprite()
{
	if( _pSprite == NULL )
	{
		D3DXCreateSprite( Core::GetInstance().GetD3dDevice(), &_pSprite );
	}

	return  _pSprite;
}

D3DXVECTOR2 UI::GetStartPos()
{
	return D3DXVECTOR2( _pos.x, _pos.y );
}

void UI::SetRect( int left, int top, int right, int bottom )
{
	_rect.left = left; 
	_rect.top = top;
	_rect.right = right ;
	_rect.bottom = bottom;
}

void UI::SetSize( int width, int height )
{
	_rect.right = _rect.left+width;
	_rect.bottom = _rect.top+height;
}

void UI::SetStartPos( int x, int y )
{
	_pos.x = static_cast< float >( x );
	_pos.y = static_cast< float >( y );
	_pos.z = 0;
}
